/**
 * @Author: Bobby
 * @Date: 2022-02-15 13:14:17
 * @LastEditTime: 2022-03-24 10:29:37
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\mixin\extensionMixin.js
 * @CIMC-V2
 */
import cloneDeep from 'lodash/cloneDeep'
import isArray from 'lodash/isArray'
import isFinite from 'lodash/isFinite'
import isString from 'lodash/isString'
import trim from 'lodash/trim'
import { getModel, recursiveList } from '@ve/utils/utils'
//主要 extension 组件
export default {
  props: {
    info: {
      required: true,
      type: Object,
      default: function () {
        return {}
      },
    },
    // extension 对象下 obj key
    typeKey: {
      type: String,
      default: function () {
        return ''
      },
    },
  },
  computed: {
    // 获取当前界面上组件
    $getPageModules() {
      // return cloneDeep(this.$store.state.diy.pageModules)
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      let _layerList = recursiveList([storePageInfo])
      let arr = []
      for (let i = 0; i < _layerList.length; i++) {
        arr.push({
          _id: _layerList[i]._id,
          name: _layerList[i].name,
          list: _layerList[i].pageModules,
          type: _layerList[i].type,
        })
      }
      return arr
    },
    // 获取当前界面上 table 组件
    $getPageTableModules() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      return this.recursiveListFindModules(storePageInfo, ['uiTable', 'uiPdaTable'])
    },
    $getPagePdaPaginationModules() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      return this.recursiveListFindModules(storePageInfo, 'uiPdaPagination')
    },
    $getCardModules() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      console.log('看看卡片数据', this.recursiveListFindModules(storePageInfo, 'uiPdaCard'))
      return this.recursiveListFindModules(storePageInfo, 'uiPdaCard')
    },
    // 获取当前界面上 steps 组件
    $getPageStepsModules() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      return this.recursiveListFindModules(storePageInfo, 'uiSteps')
    },
    // 获取当前界面上 form 组件
    $getPageFormModules() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)

      return this.recursiveListFindModules(storePageInfo, [
        'uiForm',
        'uiCirculationForm',
        'uiPackDetails',
        'uiPdaForm',
        'uiCardList',
        'uiLcsSystemIndexPage',
      ])
    },
    $getPageInfo() {
      return cloneDeep([this.$store.state.diy.pageInfo])
    },
    //获取当前可关闭弹框
    $getClosePage() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      let selectLayerId = this.$store.state.diy.selectLayerId
      let page = getModel([storePageInfo], selectLayerId)
      let list = []
      if (selectLayerId && page.type == 2) {
        //类型是弹框
        page.list = []
        list = [page]
      }
      return list
    },
    // 获取当前页面所有图层
    $getLayers() {
      let storePageInfo = cloneDeep(this.$store.state.diy.pageInfo)
      //获取选择的页面图层id
      // let selectLayerId = this.$store.state.diy.selectLayerId
      // let page = getModel([storePageInfo], selectLayerId)
      // console.log('storePageInfo', storePageInfo)
      // //清空内部list 只保留当前选择下级
      // let list = []
      // if (page.list) {
      //   list = page.list
      //   list.forEach((v) => {
      //     if (v.list) {
      //       v.list = []
      //     }
      //   })
      // }
      return [storePageInfo]
    },
    //
    item() {
      return this.info
    },
    //v-model使用
    model: {
      get() {
        // console.log('item 666666666666666', this.item)
        // 如果组件为数值输入框 并且值类型不为有限数值 则转换数据（兼容老组件的数据格式）
        if (this.item.type === 'exInputNumber' && !isFinite(this.info.value)) {
          this.info.value = 0
        }
        return this.info.value
      },
      set(value) {
        // 如果为字符串值 则清除前后空格
        if (isString(value) && this.item.type !== 'exTextarea') {
          value = trim(value)
        }
        this.info.value = value
      },
    },
  },
  methods: {
    // 筛选当前页面组件
    recursiveListFindModules(data, UiType) {
      console.log('data, UiType', data, UiType)
      let stateList = []
      const eachFunc = (obj, UiType) => {
        if ((isArray(UiType) && UiType.includes(obj.type)) || obj.type === UiType) {
          stateList.push({
            _id: obj._id,
            name: obj.name,
          })
        }
        obj.pageModules &&
          obj.pageModules.forEach((item) => {
            eachFunc(item, UiType)
          })
        obj.list &&
          obj.list.forEach((item) => {
            eachFunc(item, UiType)
          })
      }
      eachFunc(data, UiType)
      return stateList
    },
  },
}
